
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
using WooEngine.Extensions;
using UnityEngine.EventSystems;
#endif
namespace WooEngine.UI.Base
{
    public class UIButtonImage : MonoBehaviour
    {
        public Button Btn;
        public Image Img;
        public Text Txt;


#if UNITY_EDITOR
        [MenuItem("GameObject/UI/UIButtonImage", false, 3001)]
        private static void OnCreateUIComponent()
        {
            var view = new GameObject("UIButtonImage").GetOrAddComponent<RectTransform>();
            view.gameObject.layer = LayerMask.NameToLayer("UI");
#if UNITY_2021_2_OR_NEWER
            if (UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null)
#else
            if (UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null)
#endif
            {
                if (Selection.activeGameObject != null)
                    view.SetParent(Selection.activeGameObject.transform, false);
            }
            else
            {
                var canvas = FindObjectOfType<Canvas>();
                if (canvas == null)
                {
                    canvas = new GameObject("Canvas").AddComponent<Canvas>();
                    canvas.renderMode = RenderMode.ScreenSpaceOverlay;

                    canvas.GetOrAddComponent<CanvasScaler>();
                    canvas.GetOrAddComponent<GraphicRaycaster>();
                }

                var eventsystem = FindObjectOfType<EventSystem>();
                if (eventsystem == null)
                {
                    eventsystem = new GameObject("EventSystem").AddComponent<EventSystem>();
                    eventsystem.GetOrAddComponent<StandaloneInputModule>();
                }

                GameObject go = Selection.activeGameObject;
                if (go != null && go.TryGetComponent<RectTransform>(out RectTransform rectTransform))
                {
                    view.SetParent(go.transform, false);
                }
                else
                {
                    view.SetParent(canvas.transform, false);
                }
            }



            var comp = view.GetOrAddComponent<UIButtonImage>();
            comp.Btn = new GameObject("Button").GetOrAddComponent<Button>();
            comp.Btn.transform.SetParent(view);
            comp.Btn.transform.localScale = Vector3.one;
            comp.Btn.transform.localPosition = Vector3.zero;
            comp.Btn.image = comp.Btn.GetOrAddComponent<Image>();
            comp.Btn.gameObject.layer = LayerMask.NameToLayer("UI");

            comp.Img = new GameObject("Image").GetOrAddComponent<Image>();
            comp.Img.transform.SetParent(view);
            comp.Img.transform.localScale = Vector3.one;
            comp.Img.transform.localPosition = Vector3.zero;
            comp.Img.gameObject.layer = LayerMask.NameToLayer("UI");
            comp.Img.raycastTarget = false;

            comp.Txt = new GameObject("Text").GetOrAddComponent<Text>();
            comp.Txt.transform.SetParent(view);
            comp.Txt.transform.localScale = Vector3.one;
            comp.Txt.transform.localPosition = Vector3.zero;
            comp.Txt.gameObject.layer = LayerMask.NameToLayer("UI");
            comp.Txt.raycastTarget = false;
        }
#endif
    }
}